package com.jy.adscm.zltx.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.*;
import com.jy.adscm.zltx.mapper.mysql.*;
import com.jy.adscm.zltx.service.JyDrawingListService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 图纸清单表 服务实现类
 * </p>
 *
 * @author zyl
 * @since 2018-10-26
 */
@Service
public class JyDrawingListServiceImpl extends ServiceImpl<JyDrawingListMapper, JyDrawingList> implements JyDrawingListService {

    @Autowired
    JyDrawingListMapper jyDrawingListMapper;
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    JyDrawingAnnexMapper jyDrawingAnnexMapper;
    @Autowired
    JyDrawingRelationMapper jyDrawingRelationMapper;
    @Autowired
    JyDrawingTypeMapper  jyDrawingTypeMapper;
    @Autowired
    JyDrawingRoleMapper jyDrawingRoleMapper;
    @Autowired
    JyDrawingListService jyDrawingListService;
    Logger logger = LoggerFactory.getLogger(getClass());


    @Override
    public List<JyDrawingList> selectAll() throws AdscmSystemException {

        logger.info("查询所有图纸清单列表...");

        Wrapper<JyDrawingList> wrapper = new EntityWrapper<>();
        wrapper.eq("parent_id",0);

        List<JyDrawingList> topList = jyDrawingListMapper.selectList(wrapper);
//        List<JyDrawingList> topList = jyDrawingListMapper.selectByParentId(0L);

        if (topList != null && topList.size()>0){
            for (JyDrawingList jyDrawingList : topList) {
                Integer drawingType = jyDrawingList.getDrawingType();
                String drawingName = jyDrawingListMapper.selectDrawingTypeName(drawingType);
                jyDrawingList.setDrawingName(drawingName + "：" + jyDrawingList.getDrawingName());
                List<JyDrawingList> children = jyDrawingListMapper.selectByParentId(jyDrawingList.getId());
                Wrapper<JyDrawingRelation> wrapper1 = new EntityWrapper<>();
                wrapper.eq("relation_id", jyDrawingList.getId());
                List<JyDrawingRelation> jyDrawingRelations = jyDrawingRelationMapper.selectList(wrapper1);
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyDrawingRelations,null)){
                    if (jyDrawingRelations.size()>0){
                        jyDrawingList.setHasSame("1");
                    }else {
                        jyDrawingList.setHasSame("0");
                    }
                }else {
                    jyDrawingList.setHasSame("0");
                }
            }
        }

        getAllChildren(topList);

        return topList;
    }

    @Override
    public JyDrawingList selectJyDrawingById(Integer id) throws AdscmSystemException {

        if (id != null){
            JyDrawingList jyDrawingList = jyDrawingListMapper.selectById(id);

            Integer drawingCreater = jyDrawingList.getDrawingCreater();
            if (drawingCreater != null ){
                JyUser user = jyUserMapper.selectById(drawingCreater);
                if (user != null){
                    jyDrawingList.setDrawingCreater_name(user.getName());
                }

            }
            Integer drawingPublisher = jyDrawingList.getDrawingPublisher();

            if (drawingPublisher != null ){
                JyUser user = jyUserMapper.selectById(drawingPublisher);
                if (user != null){
                    jyDrawingList.setDrawingPublisher_name(user.getName());
                }
            }

            Integer drawingType = jyDrawingList.getDrawingType();
            if (drawingType != null ){
                String drawingTypeDesc = jyDrawingListMapper.selectDrawingTypeName(drawingType);
                jyDrawingList.setDrawingType_desc(drawingTypeDesc);
            }

            JyUser user = jyUserMapper.selectById(drawingCreater);

            //查询附件
            Wrapper<JyDrawingAnnex> wrapper = new EntityWrapper<>();
            wrapper.eq("drawing_id",jyDrawingList.getId());
            wrapper.eq("annex_type",1);
            List<JyDrawingAnnex> jyDrawingAnnexes = jyDrawingAnnexMapper.selectList(wrapper);

            jyDrawingList.setAnnexes(jyDrawingAnnexes);

            return jyDrawingList;
        }

        throw new AdscmSystemException("ID 为空，获取失败");
    }
  @Transactional
    @Override
    public Boolean insertJyDrawingListTree(JyDrawingList jyDrawingList) throws AdscmSystemException {
        if (!AdscmBaseUtils.getInstance().isNotEmpty(jyDrawingList,null)){
            throw  new AdscmSystemException("数据为空");
        }
        Long parentId = jyDrawingList.getParentId();
        if (parentId==0){
            JyDrawingType jyDrawingType=new JyDrawingType();
            jyDrawingType.setName(jyDrawingList.getDrawingRemark());
            jyDrawingList.setDrawingRemark(null);
            jyDrawingTypeMapper.insert(jyDrawingType);
            jyDrawingList.setDrawingType(jyDrawingType.getId());
        }else {
            JyDrawingList jyDrawingList1 = jyDrawingListMapper.selectById(jyDrawingList.getParentId());
            jyDrawingList.setDrawingType(jyDrawingList1.getDrawingType());
        }
        jyDrawingList.setCreateTime(new Date());
        jyDrawingList.setDrawingPrice("0");
        jyDrawingList.setAddFlag("0");
        jyDrawingList.setDrawingNeedTrain("0");
        jyDrawingList.setDrawingIsPublish("0");
        boolean insert = super.insert(jyDrawingList);
        return insert;
    }
    @Transactional
    @Override
    public Boolean deleteJyDrawingListTree(Integer id) throws AdscmSystemException {
        JyDrawingList jyDrawingList = super.selectById(id);
        if ("1".equals(jyDrawingList.getAddFlag())){
            throw  new AdscmSystemException("不能删除");
        }
        Wrapper<JyDrawingList> wrapper =new EntityWrapper<>();
        wrapper.eq("parent_id",jyDrawingList.getId());
        List<JyDrawingList> jyDrawingLists = super.selectList(wrapper);
         if (jyDrawingLists.size()>0){
             throw  new AdscmSystemException("不能删除");
         }
         if (jyDrawingList.getParentId()==0){
             jyDrawingTypeMapper.deleteById(Integer.valueOf(jyDrawingList.getDrawingType()));
         }
        boolean b = super.deleteById(Long.valueOf(String.valueOf(id)));
        return b;
    }
    @Override
    public Boolean updateJyDrawingListTree(JyDrawingList jyDrawingList) throws AdscmSystemException {
        jyDrawingListMapper.updateJyDrawingList(jyDrawingList);
      //  boolean b = super.updateById(jyDrawingList);
        return true;
    }

    @Override
    public List<JyDrawingList> selectRelationDraw(Integer id) throws AdscmSystemException {
        //查询这个关联的图纸id号
        List<JyDrawingList> jyDrawingLists=new ArrayList<>();
        Wrapper<JyDrawingRelation> wrapper = new EntityWrapper<>();
        wrapper.eq("relation_id",id);
        List<JyDrawingRelation> jyDrawingRelations = jyDrawingRelationMapper.selectList(wrapper);
        if (jyDrawingRelations.size()>0){
            JyDrawingRelation relation = jyDrawingRelations.get(0);
            Wrapper<JyDrawingRelation> jyDrawingRelationWrapper = new EntityWrapper<>();
            jyDrawingRelationWrapper.eq("drawing_id",relation.getDrawingId());
            List<JyDrawingRelation> jyDrawingRelations1 = jyDrawingRelationMapper.selectList(jyDrawingRelationWrapper);
            if (jyDrawingRelations1.size()>0){
                for (JyDrawingRelation jyDrawingRelation : jyDrawingRelations1) {
                    JyDrawingList jyDrawingList = super.selectById(Integer.valueOf(jyDrawingRelation.getRelationId()));
                    jyDrawingLists.add(jyDrawingList);
                }
                JyDrawingList jyDrawingList = super.selectById(id);
                jyDrawingLists.add(jyDrawingList);
            }
        }
        return jyDrawingLists;
    }

    @Override
    public List<JyDrawingList> selectRelation(Integer id) {
        List<JyDrawingList> jyDrawingLists=new ArrayList<>();
        Wrapper<JyDrawingRelation> wrapper = new EntityWrapper<>();
        wrapper.eq("relation_id",id);
        List<JyDrawingRelation> jyDrawingRelations = jyDrawingRelationMapper.selectList(wrapper);
        if (jyDrawingRelations.size()>0){
            JyDrawingRelation relation = jyDrawingRelations.get(0);
            Wrapper<JyDrawingRelation> jyDrawingRelationWrapper = new EntityWrapper<>();
            jyDrawingRelationWrapper.eq("drawing_id",relation.getDrawingId());
            List<JyDrawingRelation> jyDrawingRelations1 = jyDrawingRelationMapper.selectList(jyDrawingRelationWrapper);

            if (jyDrawingRelations1.size()>0){
                for (JyDrawingRelation jyDrawingRelation : jyDrawingRelations1) {
                    JyDrawingList jyDrawingList = super.selectById(Integer.valueOf(jyDrawingRelation.getRelationId()));
                    if (AdscmBaseUtils.getInstance().isNotEmpty(jyDrawingList.getDrawingType(),null)){
                        JyDrawingType jyDrawingType = jyDrawingTypeMapper.selectById(jyDrawingList.getDrawingType());
                        if (AdscmBaseUtils.getInstance().isNotEmpty(jyDrawingType,null)){
                            jyDrawingList.setDrawingTypeName(jyDrawingType.getName());
                        }
                    }
                    jyDrawingLists.add(jyDrawingList);
                }
            }
        }

        return jyDrawingLists;

    }
    @Transactional
    @Override
    public Boolean updateJyDrawing(Integer id) {
        JyDrawingList jyDrawingList = super.selectById(id);
        jyDrawingList.setUpdateTime(new Date());
        boolean b = super.updateById(jyDrawingList);
        return b;
    }

    @Override
    public List<JyDrawingList> selectAllByFilter(Token token) throws AdscmSystemException {
        List<JyDrawingList> jyDrawingLists = jyDrawingListService.selectAll();
        List<JyDrawingList> result=new ArrayList<>();
        Boolean jq = jyUserMapper.queryUserIsHasRole(token.getUser().getId().toString(), "194");
        //机器
        if (jq){
            Wrapper<JyDrawingRole> wrapper=new EntityWrapper<>();
            wrapper.eq("roleId",194);
            List<JyDrawingRole> jyDrawingRoles = jyDrawingRoleMapper.selectList(wrapper);
            for (JyDrawingRole jyDrawingRole : jyDrawingRoles) {
                for (JyDrawingList jyDrawingList : jyDrawingLists) {
                    if (  String.valueOf(jyDrawingList.getId()).equals( String.valueOf(jyDrawingRole.getDrawingId()))){
                        result.add(jyDrawingList);
                    }
                }
            }
        }
        Boolean hc = jyUserMapper.queryUserIsHasRole(token.getUser().getId().toString(), "195");
       if (hc){
           Wrapper<JyDrawingRole> wrapper=new EntityWrapper<>();
           wrapper.eq("roleId",195);
           List<JyDrawingRole> jyDrawingRoles = jyDrawingRoleMapper.selectList(wrapper);
           for (JyDrawingRole jyDrawingRole : jyDrawingRoles) {
               for (JyDrawingList jyDrawingList : jyDrawingLists) {
                   if (String.valueOf(jyDrawingList.getId()).equals( String.valueOf(jyDrawingRole.getDrawingId()))){
                       result.add(jyDrawingList);
                   }
               }
           }
       }

        return result;
    }

    @Override
    public List<JyDrawingList> selectByParentId(Integer id, Token token) {
        //根据父级id
        List<JyDrawingList> result=new ArrayList<>();
         if (id==0){
            Wrapper<JyDrawingList> jyDrawingListWrapper=new EntityWrapper<>();
             jyDrawingListWrapper.eq("parent_id",0);
             jyDrawingListWrapper.orderBy("rank",false);
             List<JyDrawingList> jyDrawingLists = super.selectList(jyDrawingListWrapper);
             for (JyDrawingList jyDrawingList : jyDrawingLists) {
                 Integer drawingType = jyDrawingList.getDrawingType();
                 String drawingName = jyDrawingListMapper.selectDrawingTypeName(drawingType);
                 jyDrawingList.setDrawingName(drawingName + "：" + jyDrawingList.getDrawingName());
             }

             result=jyDrawingLists;
//             Boolean jq = jyUserMapper.queryUserIsHasRole(token.getUser().getId().toString(), "194");
//             //机器
//             if (jq){
//                 Wrapper<JyDrawingRole> wrapper=new EntityWrapper<>();
//                 wrapper.eq("roleId",194);
//                 List<JyDrawingRole> jyDrawingRoles = jyDrawingRoleMapper.selectList(wrapper);
//                 for (JyDrawingRole jyDrawingRole : jyDrawingRoles) {
//                     for (JyDrawingList jyDrawingList : jyDrawingLists) {
//                         if (  String.valueOf(jyDrawingList.getId()).equals( String.valueOf(jyDrawingRole.getDrawingId()))){
//                             result.add(jyDrawingList);
//                         }
//                     }
//                 }
//             }
//             Boolean hc = jyUserMapper.queryUserIsHasRole(token.getUser().getId().toString(), "195");
//             if (hc){
//                 Wrapper<JyDrawingRole> wrapper=new EntityWrapper<>();
//                 wrapper.eq("roleId",195);
//                 List<JyDrawingRole> jyDrawingRoles = jyDrawingRoleMapper.selectList(wrapper);
//                 for (JyDrawingRole jyDrawingRole : jyDrawingRoles) {
//                     for (JyDrawingList jyDrawingList : jyDrawingLists) {
//                         if (String.valueOf(jyDrawingList.getId()).equals( String.valueOf(jyDrawingRole.getDrawingId()))){
//                             result.add(jyDrawingList);
//                         }
//                     }
//                 }
//             }


         }else {
             Wrapper<JyDrawingList> jyDrawingListWrapper=new EntityWrapper<>();
             jyDrawingListWrapper.eq("parent_id",id);
             jyDrawingListWrapper.orderBy("rank",false);
             result = super.selectList(jyDrawingListWrapper);
             for (JyDrawingList jyDrawingList : result) {
                 Wrapper<JyDrawingRelation> wrapper =new EntityWrapper<>();
                 wrapper.eq("relation_id",jyDrawingList.getId());
                 List<JyDrawingRelation> jyDrawingRelations = jyDrawingRelationMapper.selectList(wrapper);
                 if (jyDrawingRelations!=null && jyDrawingRelations.size()>0){
                     jyDrawingList.setHasSame("1");
                 }

             }
     }
        return result;
    }

    @Override
    public List<JyDrawingList> selectByDrawType(Integer id, Token token) {
        Wrapper<JyDrawingList> wrapper=new EntityWrapper<>();
        wrapper.eq("drawing_type",id);
        List<JyDrawingList> jyDrawingLists = super.selectList(wrapper);
        return jyDrawingLists;
    }

    @Override
    public List<JyDrawingList> selectByKeyWords(String keyWords) {
        Wrapper<JyDrawingList> wrapper=new EntityWrapper<>();
        wrapper.like("drawing_name",keyWords);
        List<JyDrawingList> jyDrawingLists = super.selectList(wrapper);
        return jyDrawingLists;
    }

    private void getAllChildren(List<JyDrawingList> parentList){

        if (parentList != null && parentList.size()>0){
            for (JyDrawingList jyDrawingList : parentList) {
//                Wrapper<JyDrawingList> wrapper = new EntityWrapper<>();
//                wrapper.eq("parent_id",jyDrawingList.getId());
//
//                List<JyDrawingList> children = jyDrawingListMapper.selectList(wrapper);
                List<JyDrawingList> children = jyDrawingListMapper.selectByParentId(jyDrawingList.getId());
                Wrapper<JyDrawingRelation> wrapper = new EntityWrapper<>();
                wrapper.eq("relation_id", jyDrawingList.getId());
                List<JyDrawingRelation> jyDrawingRelations = jyDrawingRelationMapper.selectList(wrapper);
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyDrawingRelations,null)){
                    if (jyDrawingRelations.size()>0){
                        jyDrawingList.setHasSame("1");
                    }else {
                        jyDrawingList.setHasSame("0");
                    }
                }else {
                    jyDrawingList.setHasSame("0");
                }
                 if (AdscmBaseUtils.getInstance().isNotEmpty(jyDrawingList.getUpdateTime(),null)){
                       if (new Date().getTime()-jyDrawingList.getUpdateTime().getTime()<15*24*60*60*1000){
                           jyDrawingList.setIsUpdate("1");
                       }else {
                           jyDrawingList.setIsUpdate("0");
                       }
                 }else {
                     jyDrawingList.setIsUpdate("0");
                 }
                if (children != null && children.size()>0){
                    jyDrawingList.setChildren(children);
                    getAllChildren(children);
                }

            }
        }


    }




}
